﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Input;
using SunXNA.RainbowXNA;

namespace Darwin.Source.Components.World
{
    class CEntityControllerPlayer : CEntityController
    {
        private Entities.CCursor m_cursor;
        private GamePadState m_previousController;  
        private CRainbowXNA.FontHandleType m_font;

        const float CostOfGuyPerson = 20.0f;

        public Entities.CCursor Cursor
        {
            get
            {
                return m_cursor;
            }

            set
            {
                m_cursor = value;
            }
        }

        public CEntityControllerPlayer()
        {
            m_cursor = new Darwin.Source.Components.World.Entities.CCursor();
            m_cursor.Position = Vector3.Zero;

            m_previousController = GamePad.GetState(PlayerIndex.One);

            m_font = Singleton<CRainbowXNA>.Instance.CreateFont("GameData/Fonts/DefaultFont");
        }

        public override void Step(Darwin.Source.Components.World.Team.CTeam myTeam, GameTime gameTime)
        {
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

            Vector3 displaceMent = Vector3.Zero;

            displaceMent.X += GamePad1.getAxisLeftStickP1().X;
            displaceMent.Z -= GamePad1.getAxisLeftStickP1().Y;

            m_cursor.Position += displaceMent;

            if (gamePadState.Buttons.A == ButtonState.Pressed && m_previousController.Buttons.A == ButtonState.Released)
            {
                if (myTeam.CurrentResources >= CostOfGuyPerson)
                {
                    Entities.MinionTypes.CResourceGatherer newGatherer = null;

                    newGatherer = myTeam.SpawnEntity<Entities.MinionTypes.CResourceGatherer>(myTeam.getHeadQuaters().Position);
                    if (newGatherer != null)
                    {
                        newGatherer.Initialise(myTeam);
                        myTeam.CurrentResources -= CostOfGuyPerson;
                    }
                }
            }

            if (gamePadState.DPad.Right == ButtonState.Pressed && m_previousController.DPad.Right == ButtonState.Released)
            {
                myTeam.AddPotentialFeild(   Darwin.Source.Components.World.PotentialFeild.FeildType.Home,
                                            m_cursor.Position,
                                            -1.0f,
                                            100.0f);
            }

            if (gamePadState.DPad.Left == ButtonState.Pressed && m_previousController.DPad.Left == ButtonState.Released)
            {
                myTeam.AddPotentialFeild(Darwin.Source.Components.World.PotentialFeild.FeildType.Resources,
                                            m_cursor.Position,
                                            -1.0f,
                                            100.0f);
            }

            if (gamePadState.Buttons.B == ButtonState.Pressed && m_previousController.Buttons.B == ButtonState.Released)
            {
                myTeam.SpawnBuilding<Entities.BuildingTypes.CTurret>(m_cursor.Position);
            }

            m_previousController = gamePadState;
        }

        public override void Draw(Darwin.Source.Components.World.Team.CTeam myTeam)
        {
            m_cursor.Draw();
        }

        public override void Draw2D(Darwin.Source.Components.World.Team.CTeam myTeam)
        {
            Singleton<CRainbowXNA>.Instance.RenderText(m_font, ((int)myTeam.CurrentResources).ToString(), Vector2.One, Color.Red);
        }
    }
}
